package com.controller;

import com.entity.BusinessUtilityBill;
import com.service.BusinessUtilityBillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 【经营项目】水电费管理
 */

@RestController
@RequestMapping("/api/business/utility/bill")
public class BusinessUtilityBillController {

    @Autowired
    private BusinessUtilityBillService businessUtilityBillService;

    @PostMapping
    public BusinessUtilityBill createBusinessUtilityBill(@RequestBody BusinessUtilityBill businessUtilityBill) {
        return businessUtilityBillService.save(businessUtilityBill);
    }

    @DeleteMapping("/{id}")
    public void deleteBusinessUtilityBill(@PathVariable Integer id) {
        businessUtilityBillService.deleteById(id);
    }

    @PutMapping("/{id}")
    public BusinessUtilityBill updateBusinessUtilityBill(@PathVariable Integer id, @RequestBody BusinessUtilityBill businessUtilityBill) {
        businessUtilityBill.setId(id);
        return businessUtilityBillService.update(businessUtilityBill);
    }

    @GetMapping("/{id}")
    public BusinessUtilityBill getBusinessUtilityBillById(@PathVariable Integer id) {
        return businessUtilityBillService.findById(id);
    }

    @GetMapping
    public List<BusinessUtilityBill> getAllBusinessUtilityBills() {
        return businessUtilityBillService.findAll();
    }

    @GetMapping("/page")
    public Page<BusinessUtilityBill> getBusinessUtilityBillsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        Pageable pageable = PageRequest.of(pageNum - 1, pageSize);
        return businessUtilityBillService.findAll(pageable);
    }
}